package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.TopicCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.TopicService;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @GetMapping("word")
    public Result checkWord(String word) {
        VocDTO vocDTO = topicService.checkWord(word);
        return Result.ok(vocDTO);
    }
    @GetMapping
    public Result<ReviewTopicDTO> getTopic(Long topicId) {
        ReviewTopicDTO topic = topicService.getTopic(topicId);
        return Result.ok(topic);
    }

    @PostMapping
    public Result<ReviewTopicDTO> addTopic(@RequestBody TopicCommand command) {
        topicService.addTopic(command);
        return Result.ok();
    }

    @GetMapping("singleChoice/list")
    public Result<PageDTO<ReviewTopicDTO>> getSingleChoicePage(VocPageRequest vocPageRequest) {
        PageDTO<ReviewTopicDTO> singleChoicePage = topicService.getSingleChoicePage(vocPageRequest);
        return Result.ok(singleChoicePage);
    }

    @GetMapping("fillWord/list")
    public Result<PageDTO<ReviewTopicDTO>> getFillWordPage(VocPageRequest vocPageRequest) {
        PageDTO<ReviewTopicDTO> fillWordPage = topicService.getFillWordPage(vocPageRequest);
        return Result.ok(fillWordPage);
    }


    @PutMapping
    public Result updateTopic(@RequestBody TopicCommand command){
        topicService.updateTopic(command);
        return Result.ok();
    }


    @DeleteMapping("{topicId}")
    public Result<DeleteTopicOperateDTO> deleteTopic(@PathVariable("topicId") Long topicId, Integer topicType){
        DeleteTopicOperateDTO deleteTopicOperateDTO = topicService.deleteTopic(topicId, topicType);
        return Result.ok(deleteTopicOperateDTO);
    }

    @DeleteMapping("option/{id}")
    public Result<DeleteTopicOptionOperateDTO> deleteTopicOption(@PathVariable("id") Long id){
        DeleteTopicOptionOperateDTO deleteTopicOptionOperateDTO = topicService.deleteTopicOption(id);
        return Result.ok(deleteTopicOptionOperateDTO);
    }


    /**
     * excel 部分
     */

    @GetMapping("template/{type}")
    public ResponseEntity<byte[]> downloadTemplate(@Parameter(description = "模版文件类型")@PathVariable("type") String type) throws IOException {
        // 使用ClassPathResource读取资源文件

        ClassPathResource resource;

        if ("single".equals(type)) {
            resource  = new ClassPathResource("templates/单选题模版.xlsx");
        } else if ("fill".equals(type)) {
            resource = new ClassPathResource("templates/填空题模版.xlsx");
        } else {
            resource = new ClassPathResource("templates/单词上传模版.xlsx");
        }
        // 检查文件是否存在
        if (!resource.exists()) {
            return ResponseEntity.notFound().build();
        }

        // 读取文件内容
        InputStream inputStream = resource.getInputStream();
        byte[] fileBytes = IOUtils.toByteArray(inputStream);

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment",
                URLEncoder.encode(resource.getFilename(), "UTF-8"));

        return ResponseEntity.ok()
                .headers(headers)
                .body(fileBytes);
    }

    /**
     * excel 校验
     * @param type   上传过来的文件类型
     * @param file   上传过来 的 文件
     * @return
     */
    @PostMapping("excel/validation/{type}")
    public Result topicExcelValidate(String type, MultipartFile file) throws IOException {
        topicService.topicExcelValidate(type,file);
        return Result.ok();
    }

    @PostMapping("singleChoice")
    public Result<ExcelAddOperateDTO> addSinleChoiceExcel(MultipartFile file, Long employeeId) throws IOException {
        ExcelAddOperateDTO excelAddOperateDTO = topicService.addSinleChoiceExcel(file, employeeId);
        return Result.ok(excelAddOperateDTO);
    }

    @PostMapping("fillWord")
    public Result<ExcelAddOperateDTO> addFillWordExcel(MultipartFile file, Long employeeId) throws IOException {
        ExcelAddOperateDTO excelAddOperateDTO = topicService.addFillWordExcel(file, employeeId);
        return Result.ok(excelAddOperateDTO);
    }


}
